﻿using Net5ApiANdWpfClient.Lib.Model;
using Net5ApiANdWpfClient.Lib.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zhjt.Comm.Lib.Tools.WebTools;

namespace Net5ApiANdWpfClient.Lib.Common
{
    public class HttpRequestTools
    {
        public static string DefaultApiUrl = PcConfig.ApiUrl;
        public static ResultModel Get(string url)
        {
            try
            {
                if (string.IsNullOrEmpty(url))
                {
                    return new ResultModel()
                    {
                        code = ResultCode.Error,
                        mess = "参数错误"
                    };
                }
                url = DefaultApiUrl + "/" + url;

                return HttpClientTools.Get<ResultModel>(url);
            }
            catch (Exception ex)
            {
                return new ResultModel()
                {
                    code = ResultCode.Error,
                    mess = ex.ToString()
                };
            }
        }

        public static ResultModel GetByToken(string url)
        {

            try
            {
                if (string.IsNullOrEmpty(url))
                {
                    return new ResultModel()
                    {
                        code = ResultCode.Error,
                        mess = "参数错误"
                    };
                }
                string token = AdminUserService.Token;
                if (string.IsNullOrEmpty(token))
                {
                    return new ResultModel()
                    {
                        code = ResultCode.Error,
                        mess = "没有权限"
                    };
                }
                url = DefaultApiUrl + "/" + url;
                Dictionary<string, string> header = new Dictionary<string, string> { { "X-Token", token } };
                return HttpClientTools.Get<ResultModel>(url, header: header);
            }
            catch (Exception ex)
            {
                return new ResultModel()
                {
                    code = ResultCode.Error,
                    mess = ex.ToString()
                };
            }
        }

        public static ResultModel Post(string url, string postData = "")
        {
            if (string.IsNullOrEmpty(url))
            {
                return new ResultModel()
                {
                    code = ResultCode.Error,
                    mess = "参数错误"
                };
            }
           try
            {
                url = DefaultApiUrl + "/" + url;
                return HttpClientTools.Post<ResultModel>(url, postData);
            }
            catch (Exception ex)
            {
                return new ResultModel()
                {
                    code = ResultCode.Error,
                    mess = ex.ToString()
                };
            }
        }

        public static ResultModel PostByToken(string url, string postData = "")
        {
            if (string.IsNullOrEmpty(url))
            {
                return new ResultModel()
                {
                    code = ResultCode.Error,
                    mess = "参数错误"
                };
            }
           try
            {
                string token = AdminUserService.Token;
                if (string.IsNullOrEmpty(token))
                {
                    return new ResultModel()
                    {
                        code = ResultCode.Error,
                        mess = "没有权限"
                    };
                }
                url = DefaultApiUrl + "/" + url;
                Dictionary<string, string> header = new Dictionary<string, string> { { "X-Token", token } };
                return HttpClientTools.Post<ResultModel>(url, postData, header: header);
            }
            catch (Exception ex)
            {
                return new ResultModel()
                {
                    code = ResultCode.Error,
                    mess = ex.ToString()
                };
            }

        }








    }
}
